package com.haierp.controller.authority;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.haierp.model.authority.RoleHai;
import com.haierp.model.authority.UserRoleHai;
import com.haierp.service.authority.IRoleServiceHai;
import com.haierp.service.authority.IUserRoleServiceHai;
import com.haierp.util.JsonPageResult;
import com.haierp.util.JsonResult;
import com.haierp.vo.authority.RoleQueryVO;

/**
 * 
 * Title: RoleController.java
 * Description: 
 *
 * @author jc
 * Apr 20, 2017
 *
 */
@Service("roleControllerHai")
@Controller
@RequestMapping("roleHai")
public class RoleControllerHai {

	@Autowired
	private IRoleServiceHai iRoleServiceHai;
	
	@Autowired
	private IUserRoleServiceHai iUserRoleServiceHai;

	@RequestMapping("/add")
	@ResponseBody
	public Object add(RoleHai role) {
		JsonResult<String> result = new JsonResult<>();
		
		if (null == role.getId()) {
			RoleQueryVO roleQueryVO = new RoleQueryVO();
			roleQueryVO.setName(role.getName());
			Integer count = iRoleServiceHai.queryRolesCountByName(role.getName(), null);
	        if (count != null && count > 0) {
	            result.buildMsg("角色名已存在!").buildIsSuccess(false);
	        } else {
		        iRoleServiceHai.insert(role);
		        result.buildIsSuccess(true);
			}
		} else {
			result.buildMsg("错误数据").buildIsSuccess(false);
		}
		
		return result;
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public Object update(RoleHai role) {
		JsonResult<String> result = new JsonResult<>();
		
		if (null != role.getId()) {
			Integer count = iRoleServiceHai.queryRolesCountByName(role.getName(), role.getId());
	        if (count != null && count > 0) {
	            result.buildMsg("角色名已存在!").buildIsSuccess(false);
	        } else {
		        iRoleServiceHai.updateById(role);
		        result.buildIsSuccess(true);
			}
		} else {
			result.buildMsg("错误数据").buildIsSuccess(false);
		}
		
		return result;
	}

	@RequestMapping("/delete")
	@ResponseBody
	public Object delete(Long id) {
		JsonResult<String> result = new JsonResult<>();

		if (null != id) {			
			UserRoleHai userRole = new UserRoleHai();
			userRole.setRoleId(id);
			EntityWrapper<UserRoleHai> entityWrapper = new EntityWrapper<UserRoleHai>(userRole);			
			Integer count = iUserRoleServiceHai.selectCount(entityWrapper);
			if (count != null && count > 0) {
	            result.buildMsg("此角色有关联的用户，无法删除!").buildIsSuccess(false);
	        } else {
	        	iRoleServiceHai.deleteRoleById(id);
				result.buildIsSuccess(true);
			}
		} else {
			result.buildMsg("错误数据").buildIsSuccess(false);
		}
		
		return result;
	}
	
	@RequestMapping("/query")
	@ResponseBody
	public Object query(Long id) {
		JsonResult<RoleHai> result = new JsonResult<>();
		
		return result.buildData(iRoleServiceHai.selectById(id)).buildIsSuccess(true);
	}
	
	@RequestMapping("/queryList")
	@ResponseBody
	public Object queryList(RoleQueryVO roleQueryVO) {
		JsonPageResult<List<RoleHai>> result = iRoleServiceHai.queryRoleList(roleQueryVO);

		return result.buildIsSuccess(true);
	}

    /**
     * 授权页面页面根据角色查询资源
     *
     * @param id
     * @return
     */
    @RequestMapping("/queryResourceIdListByRoleId")
    @ResponseBody
    public Object findResourceByRoleId(Long id) {
    	JsonResult<List<Long>> result = new JsonResult<List<Long>>();
        List<Long> resources = iRoleServiceHai.queryResourceIdListByRoleId(id);
        result.buildData(resources);
        return result;
    }

    /**
     * 授权
     *
     * @param id
     * @param resourceIds
     * @return
     */
    @RequestMapping("/updateGrant")
    @ResponseBody
    public Object updateGrant(Long id, String resourceIds) {
		JsonResult<String> result = new JsonResult<>();
        iRoleServiceHai.updateRoleResource(id, resourceIds);
		return result.buildIsSuccess(true);
    }
}
